package YubicoClient;


##  --------------------------------------------------------
##                      use/require
##  --------------------------------------------------------

use strict;
use warnings;

use WWW::Mechanize;


##  --------------------------------------------------------
##                       constants
##  --------------------------------------------------------

use constant
{
  YubicoAuthSrvURLprefix  =>  'http://api.yubico.com/wsapi/verify?id='
};


##  --------------------------------------------------------
##                      class methods
##  --------------------------------------------------------
##
##  sig  ::  (authId:str)  --->  void

sub new
{
  my $invocant = shift;
  my $class = ref($invocant) || $invocant;
  my $self = {};
  $self->{clientId}     = undef;
  $self->{HTTPresponse} = undef;
  bless($self,$class);
  return $self;
}

sub clientId
{
  my $self = shift;
  if (@_) { $self->{clientId} = shift }
  return $self->{clientId}
}

sub HTTPresponse
{
  my $self = shift;
  if (@_) { $self->{HTTPresponse} = shift }
  return $self->{HTTPresponse}
}

sub verify
{
  my $self = shift;
  my $otp  = shift;

  my $YubicoAuthSrvURL = YubicoAuthSrvURLprefix . $self->{clientId} . "&otp=" . $otp;
  my $mech = WWW::Mechanize->new();

  $mech->get($YubicoAuthSrvURL);
  $self->{HTTPresponse} = $mech->response()->content();
  if ($self->{HTTPresponse} =~ /status=OK/) { return 1 } else { return 0 }
}


1;
